package com.ywjh.inter.socket;

import com.ywjh.inter.entity.CholesterolEntity;
import com.ywjh.inter.entity.EquBindEntity;
import com.ywjh.inter.entity.SrcDataValue4G;
import com.ywjh.inter.entity.UricAcidEntity;
import com.ywjh.inter.service.BioLandService;
import com.ywjh.inter.service.EquBindService;
import com.ywjh.inter.service.PatientDataService;
import com.ywjh.inter.utils.StrUtil;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.sql.Timestamp;
import java.util.List;

/**
 * 多线程处理socket接收的数据
 * @Author: lhb
 * @Date: 2024/05/16
 */
public class SocketOperate extends Thread {
    private Socket socket;

    public SocketOperate(Socket socket) {
        this.socket=socket;
    }

    @Override
    public void run() {
        try {
            //封装输入流（接收客户端的流）
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
            DataInputStream dis = new DataInputStream(bis);
            byte[] bytes = new byte[1]; // 一次读取一个byte

            String ret = "";
            //System.out.println("转换为字符串前:"+String.valueOf(bytes));
            while (dis.read(bytes) != -1) {

                ret += MessageParsing.bytesToHexString(bytes) + "";

                if (dis.available() == 0) { //一个请求

                    String data = MessageParsing.hexStringToString(ret);
                    System.out.println("ASCII转字符串后:"+data);
                    if (BioLandService.getInstance().checkReceivedData4G(data)) {
                        SrcDataValue4G dataValue4G = new SrcDataValue4G(data);

                        List<EquBindEntity> list = EquBindService.getInstance().list(dataValue4G.getSn(),
                                EquBindService.TYPE_BGLU);
                        int userId = 0;
                        if (!list.isEmpty()) {
                            EquBindEntity equBindEntity = list.get(0);
                            userId = equBindEntity.getUser_id();

                            if(("GLUC").equals(dataValue4G.getModel())){
                                BioLandService.getInstance().blglu4G(dataValue4G);

                            }else if("CHOL".equals(dataValue4G.getModel())){
                                //int userId = data.getInt("user_id");
                                float cholesterolData;
                                String dataValue = dataValue4G.getData().toUpperCase();
                                if(dataValue.equals("HIGH")){
                                    cholesterolData = StrUtil.getFloat("10.36", 0.0f);
                                }else if(dataValue.equals("LOW")){
                                    cholesterolData = StrUtil.getFloat("2.58", 0.0f);
                                }else {
                                    cholesterolData = StrUtil.getFloat(dataValue4G.getData(), 0.0f);
                                }
                                //float cholesterolData = StrUtil.getFloat(dataValue4G.getData(), 0.0f);
                                //Timestamp time = StrUtil.getTimestampHHMISS(data.getString("time"));
                                //String memo = data.getString("memo");
                                Timestamp time = StrUtil.getTimeStampYYMMDDHHMI(dataValue4G.getYear() + dataValue4G.getMonth()+dataValue4G.getDay()
                                        + dataValue4G.getHour() + dataValue4G.getMinute());
                                //5代表数据来源于设备通过4G独立上传
                                String source = "5";
                                CholesterolEntity entity = new CholesterolEntity();
                                entity.setCheck_time(time);
                                entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
                                entity.setData(cholesterolData);
                                entity.setStatus(1);
                                entity.setUser_id(userId);
                                entity.setMemo("设备通过4G经socket通信上传数据");
                                entity.setData_source(source);
                                PatientDataService.getInstance().addCholesterol(entity);

                            }else if("UA".equals(dataValue4G.getModel())){
                                //int userId = data.getInt("user_id");
                                //float uricAcidData = StrUtil.getFloat(dataValue4G.getData(), 0.0f);
                                //Timestamp time = StrUtil.getTimestampHHMISS(data.getString("time"));
                                //String memo = data.getString("memo");

                                float uricAcidData;
                                String dataValue = dataValue4G.getData().toUpperCase();
                                if(dataValue.equals("HIGH")){
                                    uricAcidData = StrUtil.getFloat("1191", 0.0f);
                                }else if(dataValue.equals("LOW")){
                                    uricAcidData = StrUtil.getFloat("179", 0.0f);
                                }else {
                                    uricAcidData = StrUtil.getFloat(dataValue4G.getData(), 0.0f);
                                }
                                Timestamp time = StrUtil.getTimeStampYYMMDDHHMI(dataValue4G.getYear() + dataValue4G.getMonth()+dataValue4G.getDay()
                                        + dataValue4G.getHour() + dataValue4G.getMinute());
                                //5代表数据来源于设备通过4G独立上传
                                String source = "5";
                                UricAcidEntity entity = new UricAcidEntity();
                                entity.setCheck_time(time);
                                entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
                                entity.setData(uricAcidData);
                                entity.setStatus(1);
                                entity.setUser_id(userId);
                                entity.setMemo("设备通过4G经socket通信上传数据");
                                entity.setData_source(source);
                                PatientDataService.getInstance().addUricAcid(entity);

                            }
                        }



                    }else {
                        System.out.println("接收数据不合法");
                    }

                    //System.out.println("转为map后的数据："+MessageParsing.stringToMap(MessageParsing.hexStringToString(ret)));

                    ret = "";
                }
            }
            //System.out.println("转换为字符串后1:"+ret);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //System.out.println("client is over");
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
